﻿using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.PowerShell;
using Microsoft.SharePoint.Administration;
using System.Globalization;
using System.ComponentModel;
using System.Security.Principal;
using System.Collections.Specialized;
using System.Linq;
using Microsoft.SharePoint.Utilities;
using System.IO;


namespace SlamCms.SharePoint.PowerShell
{
	[Cmdlet("Import", "SlamSite")]
	public class SpCmdletImportSlamSite : SPCmdlet
	{
		[Parameter(Mandatory = true)]
		public string SiteUrl
		{
			get;
			set;
		}

		[Parameter(Mandatory = true)]
		public string FileName
		{
			get;
			set;
		}

		[Parameter(Mandatory = false)]
		public SwitchParameter IncludeContent
		{
			get;
			set;
		}

		[Parameter(Mandatory = false)]
		public SwitchParameter ActivateSlam
		{
			get;
			set;
		}

		protected override void InternalProcessRecord()
		{
			try
			{
				if (ActivateSlam)
				{
					FileInfo fileInfo = new FileInfo(FileName);

					AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", Path.Combine(fileInfo.DirectoryName, "app.config"));
				}
				SPSecurity.RunWithElevatedPrivileges(delegate()
				{
					Migration.MigrationService.Import(SiteUrl, FileName, IncludeContent.IsPresent, ActivateSlam.IsPresent, (message) => this.WriteVerbose(message));
				});
			}
			catch (Exception ex)
			{
				WriteResult(ex.ToString());
			}
		}
	}
}
